package maf;

import static robocode.util.Utils.normalRelativeAngleDegrees;
import java.awt.Color;
import robocode.HitByBulletEvent;
import robocode.HitRobotEvent;
import robocode.Robot;
import robocode.ScannedRobotEvent;

/**
 * CompetitiveRobot: A simple and competitive robot.
 * 
 * @author Michael Fujimoto
 */
public class CompetitiveRobot extends Robot {
  double gunTurnAmt; // How much to turn our gun when searching
  //String targetName; // The name of our current target
  //Double firePower; // Fire power adjusted by distance
  Double moveAmount; // Amount of pixels to move
  //double nearestTargetDistance; // Used to target the closest robot
  int cornerCounter = 0;
  int turnAroundCount = 0;
  
  /**
   * Tries to follow the walls and track a target.
   * Robot will advance and fire if a target is found 
   *
   */
  public void run() {
    //nearestTargetDistance = Math.max(getBattleFieldHeight(), getBattleFieldWidth());
    // Set colors
    setBodyColor(new Color(128, 128, 50));
    setGunColor(new Color(50, 50, 20));
    setRadarColor(new Color(200, 200, 70));
    setScanColor(Color.white);
    setBulletColor(Color.blue);
    setAdjustGunForRobotTurn(true);
    // Prepare gun
    //targetName = null; // Initialize to not tracking anyone
    moveAmount = Math.max(getBattleFieldWidth(), getBattleFieldHeight());
    // turnLeft to face a wall.
    // getHeading() % 90 means the remainder of
    // getHeading() divided by 90.
    // turnLeft to face a wall.
    // getHeading() % 90 means the remainder of
    // getHeading() divided by 90.
    while (getHeading() != 0) {
      turnLeft(90 % getHeading());
    }
    ahead(moveAmount);
    // Turn the gun to turn right 90 degrees.
    turnLeft(90);
    ahead(moveAmount);
    turnLeft(90);

    turnGunLeft(90);

    while (true) {
      turnGunLeft(360 % getGunHeading());
      turnGunRight(90);
      ahead(moveAmount / 2);
      turnLeft(180);
      turnGunLeft(180);
      ahead(moveAmount / 8);
    }
  }

  /**
   * Robot will advance and fire if another robot is hit.
   * 
   * @param e contains information about the enemy robot, e.g. its location
   */
  public void onHitRobot(HitRobotEvent e) {
    // Back up and fire
    gunTurnAmt = normalRelativeAngleDegrees(e.getBearing() + (getHeading() - getRadarHeading()));
    turnGunRight(gunTurnAmt);
    fire(3);
    back(50);
  }
  /**
   * Robot will target and fire when another robot is scanned.
   * 
   * @param e contains information about the enemy robot, e.g. its location
   */
  public void onScannedRobot(ScannedRobotEvent e) {
    fire(2);
    // ahead(-100);
    if (turnAroundCount > 7) {
      ahead(-50);
      turnAroundCount = 0;
    }
    gunTurnAmt = normalRelativeAngleDegrees(e.getBearing() + (getHeading() - getRadarHeading()));
    turnGunRight(gunTurnAmt);
    ahead(50);
    turnAroundCount++;
  }
  /**
   * Robot will turn the gun and return fire when hit by a bullet.
   * 
   * @param e contains information about the enemy robot, e.g. its location
   */
  public void onHitByBullet(HitByBulletEvent e) {
    if (cornerCounter > 1) {
      out.println("OH NOES");
      turnRight(getHeading() % 90);
      ahead(moveAmount / 2 * -1);
      turnRight(45);
      gunTurnAmt = normalRelativeAngleDegrees(e.getBearing() + (getHeading() - getRadarHeading()));
      turnGunRight(gunTurnAmt);
    }
    else {
      gunTurnAmt = normalRelativeAngleDegrees(e.getBearing() + (getHeading() - getRadarHeading()));
      turnGunRight(gunTurnAmt);
      turnRight(90);
      ahead(-100);
      turnRight(30);
      ahead(-200);
      cornerCounter++;
    }
  }
}
